//
//  DiseaseListViewController.m
//  TestARC
//
//  Created by wuxiaoquan on 13-4-7.
//
//

#import "DiseaseListViewController.h"
#import "CommentListViewController.h"

@interface DiseaseListViewController ()

@end

@implementation DiseaseListViewController
@synthesize queryId,name,diseaseArray,diseaseTv;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.diseaseArray == nil) {
        self.diseaseArray = [[NSArray alloc]init];
    }
    
    [self addRoundBound:self.diseaseTv];
    [self loadDiseaseList];
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadDiseaseList
{
    [self showWaitView:nil];
    
    NSArray  * array= [self.name componentsSeparatedByString:@" "];
    NSString * nameStr = [NSString stringWithFormat:@"%@  %@",[array objectAtIndex:0],[array objectAtIndex:1]];
    NSLog(@"name=%@",nameStr);
    
    int timestamp = (int)[[NSDate date]timeIntervalSince1970];
    NSString * checksum = [[NSString stringWithFormat:@"%@%dyaoxia",self.queryId,timestamp]md5];
    
    NSString *URL_DETAIL = [NSString stringWithFormat:@"http://www.yaoxia.com/ajaxServlet"];
    NSURL *url = [NSURL URLWithString:URL_DETAIL];
    request4Comment = [ASIFormDataRequest requestWithURL:url];
    //www.yaoxia.com/ajaxServlet?style=getbaseinfo_lookformobile&drugid=$1&drugname=$2&timestamp=$3&checksum=$4
    [request4Comment setPostValue:@"getbaseinfo_lookformobile" forKey:@"style"];
    [request4Comment setPostValue:self.queryId forKey:@"drugid"];
    [request4Comment setPostValue:nameStr forKey:@"drugname"];
    [request4Comment setPostValue:[NSString stringWithFormat:@"%d",timestamp]  forKey:@"timestamp"];
    [request4Comment setPostValue:checksum forKey:@"checksum"];
    NSLog(@"style=getbaseinfo_lookformobile\ndrugid=%@\ndrugname=%@\ntimestamp=%d\nchecksum=%@",self.queryId,nameStr,timestamp,checksum);
    [request4Comment setCompletionBlock:^{
        
        [self hideWaitView];
        
        // Use when fetching text data
        NSString *responseString = [request4Comment responseString];
        
        //NSLog(@"responseString = %@",responseString);
        id json = [responseString JSONValue];
        if ([[json objectForKey:@"code"]intValue] == 500) {
            NSLog(@"%@",[json objectForKey:@"message"]);
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"网络加载错误"
                                  message:@"无法连接到网络"
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
            [alert show];
            return ;
        }
        self.diseaseArray = [json objectForKey:@"array"];
        //NSLog(@"diseaseArray=%@",self.diseaseArray) ;
        
        [self.diseaseTv reloadData];
       
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request4Comment setFailedBlock:^{
        
        [self hideWaitView];
        
        NSError *error = [request4Comment error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"无法连接到网络"
                              message:nil
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request4Comment startAsynchronous];
}

//-------------------------表格数据----------------------------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.diseaseArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = nil;
    NSInteger row;
    
    cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:SimpleTableIdentifier];
    }
    row = [indexPath row];
    //cell.textLabel.text = [[self.diseaseArray objectAtIndex:row]objectForKey:@"name"];
    NSString *diseaseName = [[self.diseaseArray objectAtIndex:row]objectForKey:@"name"];
    NSString *commentNum = [[self.diseaseArray objectAtIndex:row]objectForKey:@"num"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ (%@条评论)",diseaseName,commentNum];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:17.0f];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //填装详细视图的内容。
    
    CommentListViewController * detailController = [[CommentListViewController alloc]initWithNibName:@"CommentListViewController" bundle:nil];
    
    int row = [indexPath row];
    NSLog(@"选择了 %d 行",row);
    NSDictionary *disease;
    disease = [self.diseaseArray objectAtIndex:row];
    detailController.diseaseName =       [disease objectForKey:@"name"];
    detailController.drugName =  self.name;
    detailController.queryId = self.queryId;
    [self.navigationController pushViewController:detailController animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
